﻿using NetDimension.NanUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TicketTaking.model;
using TicketTaking.Tool;

namespace TicketTaking
{

    public partial class Main : HtmlContentForm
    {
        public TicketSetting Settings { get; set; }

        public JSBridgeObject JSBridge { get; set; }

        public VBar Vbar { get; set; }

        public ICReader ICReader{ get; set;}

        public Main()
            : base("local:///www/index.html")
        {
            try
            {
                InitializeComponent();

                JSBridge = new JSBridgeObject(this);
                GlobalObject.Add("host", JSBridge);

                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
                this.Width = 1280;
                this.Height = 1024;

                Settings = TicketSetting.Init();
                if (!Settings.IsValid) MessageBox.Show("配置项不完整，请设置后重启程序");

                
                Vbar = new VBar();
                bool openSuc = Vbar.OpenDevice("127.0.0.1");
                if (openSuc)
                {
                    Vbar.Beep(1);
                    Vbar.AddSymbolType(1);
                    Task.Factory.StartNew(JSBridge.KeepScan);
                }
                else
                   MessageBox.Show("连接Z100扫描器失败，请检查后重启程序");

                // 连接ic卡读卡器
                ICReader = new ICReader();
                bool connectIC = ICReader.ConnectIC();
                if (connectIC)
                {
                    ICReader.Beep(2);

                    Task.Factory.StartNew(JSBridge.KeepScanIC);
                }
                else
                {
                    MessageBox.Show("IC卡连接失败，请检查后重启程序");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                MessageBox.Show(ex.Message);
            }
        }
    }
}